package com.oaker.hours.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.oaker.common.constant.HttpStatus;
import com.oaker.common.core.page.TableDataInfo;
import com.oaker.common.exception.ServiceException;
import com.oaker.hours.doman.columns.Columns;
import com.oaker.hours.doman.dto.PsersonTreandDTO;
import com.oaker.hours.doman.dto.UserHourSaveDTO;
import com.oaker.hours.doman.entity.*;
import com.oaker.hours.doman.vo.PersonTreandVO;
import com.oaker.hours.service.SysPersonTrendService;
import com.oaker.hours.mapper.SysPersonTrendMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.*;

/**
*
*/
@Service
public class SysPersonTrendServiceImpl extends ServiceImpl<SysPersonTrendMapper, SysPersonTrend>
implements SysPersonTrendService{

    @Autowired
    SysPersonTrendMapper sysPersonTrendMapper;

    @Autowired
    SysPersonTrendService personTrendService;

    @Override
    public TableDataInfo selectUserTreandVo(PersonTreandVO personTreandVO, int pageNum, int pageSize) {
        TableDataInfo tableDataInfo = new TableDataInfo();
        List<PersonTreandVO> personTreandVOS = sysPersonTrendMapper.selectUserTreandVo(personTreandVO);
        Integer count = personTreandVOS.size();
        if (count <= 0) {
            tableDataInfo.setCode(HttpStatus.SUCCESS);
            tableDataInfo.setRows(Collections.emptyList());
            return tableDataInfo;
        }
        tableDataInfo.setTotal(count);
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setRows(personTreandVOS);
        return tableDataInfo;
    }


    /**
     * 1. 写入mh_user_hour 用户工时填报表
     * 2. 写入mh_hour_detail工时填报详情表
     * 3. 更新mh_project_hour表中已用工时
     * 4. 如果mh_user_hour_miss用户缺报记录表 有缺报记录 则执行删除
     * @param
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean create(PsersonTreandDTO psersonTreandDTO) {
        EntityWrapper<SysPersonTrend> queryWrapper = new EntityWrapper<>();
        queryWrapper.eq(Columns.PersonTrend.date, psersonTreandDTO.getDate())
                .and()
                .eq(Columns.PersonTrend.user_id, psersonTreandDTO.getUserId());
        Integer count = baseMapper.selectCount(queryWrapper);
        if (count > 0) {
            return false;
//            throw new ServiceException("当前日期已经填报工时，请勿重复提交-->"+userHourSaveDTO.getDate()+"---用户ID:"+userHourSaveDTO.getUserId());
        }
        LocalDate fillDate = psersonTreandDTO.getDate();
        List<SysPersonTrend> detailList = new ArrayList<>();

        for (PsersonTreandDTO.ProjectHour projectHour : psersonTreandDTO.getProjectHours()) {
            SysPersonTrend trend = new SysPersonTrend();
            trend.setProjectId(projectHour.getProjectId());
            trend.setUserId(psersonTreandDTO.getUserId());
            trend.setDate(fillDate);
            trend.setDangerAct(psersonTreandDTO.getDangerAct());
            trend.setManagerAct(psersonTreandDTO.getManagerAct());
            detailList.add(trend);
        }
        personTrendService.insertBatch(detailList);
        return Boolean.TRUE;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean createBatch(List<PsersonTreandDTO> psersonTreandDTOs) {

        List<SysPersonTrend> detailList = new ArrayList<>();

        for (PsersonTreandDTO psersonTreandDTO : psersonTreandDTOs) {
            EntityWrapper<SysPersonTrend> queryWrapper = new EntityWrapper<>();
            queryWrapper.eq(Columns.PersonTrend.date, psersonTreandDTO.getDate())
                    .and()
                    .eq(Columns.PersonTrend.user_id, psersonTreandDTO.getUserId());
            Integer count = baseMapper.selectCount(queryWrapper);
            if (count > 0) {
            throw new ServiceException("该用户当前日期已经填报工时，请勿重复提交-->"+psersonTreandDTO.getDate()+"---用户ID:"+psersonTreandDTO.getUserId());
            }
            LocalDate fillDate = psersonTreandDTO.getDate();

            for (PsersonTreandDTO.ProjectHour projectHour : psersonTreandDTO.getProjectHours()) {
                SysPersonTrend trend = new SysPersonTrend();
                trend.setProjectId(projectHour.getProjectId());
                trend.setUserId(psersonTreandDTO.getUserId());
                trend.setDate(fillDate);
                trend.setDangerAct(psersonTreandDTO.getDangerAct());
                trend.setManagerAct(psersonTreandDTO.getManagerAct());
                detailList.add(trend);
            }
        }
        personTrendService.insertBatch(detailList);
        return Boolean.TRUE;
    }

}
